package servlet;


import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import commons.RedisCommons;
import dao.SaleStatisticDAO;
import entity.PageBean;
import dao.ProductDAO;
import entity.Product;
import entity.SaleStatistic;
import redis.clients.jedis.ShardedJedis;
import util.RedisUtil;
import vo.SaleStatisticVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@WebServlet(name="productList", value = "/product/list")
public class ProductListAllServlet extends HttpServlet {
    SaleStatisticVO getSaleStatistic(){
        ShardedJedis jedis = RedisUtil.getJedis();
        int year = LocalDateTime.now().getYear();
        SaleStatisticVO saleStatisticVO=new SaleStatisticVO();
//    redis中获取数据  map<月份,SaleStatistic>
        Map<String, String> stringStringMap = jedis.hgetAll(RedisCommons.SALE_STATISTIC + year);
        if(stringStringMap!=null && !stringStringMap.isEmpty()){
            for(int i=1;i<=stringStringMap.size();i++){
                String s = stringStringMap.get(i + "");
                JSONObject entries = JSONUtil.parseObj(s);
                Integer saleNum = (Integer) entries.get("saleNum");
                Integer visitNum = (Integer) entries.get("visitNum");
                saleStatisticVO.getSaleNum().add(saleNum);
                saleStatisticVO.getVisitNum().add(saleNum);
            }
        }else {
            //redis无数据,mongodb中查找
            SaleStatisticDAO statisticDAO =new SaleStatisticDAO();
            List<SaleStatistic> byYear = statisticDAO.findByYear(year);
            //缓存到redis中
            for (int i=0;i<byYear.size();i++){
                SaleStatistic saleStatistic =byYear.get(i);
                jedis.hset(RedisCommons.SALE_STATISTIC + year,
                        saleStatistic.getMonth()+"",
                        JSONUtil.toJsonStr(saleStatistic));
                saleStatisticVO.getSaleNum().add(saleStatistic.getSaleNum());
                saleStatisticVO.getVisitNum().add(saleStatistic.getVisitNum());
            }
        }
        while(saleStatisticVO.getSaleNum().size()<12){
            saleStatisticVO.getSaleNum().add(0);
            saleStatisticVO.getVisitNum().add(0);
        }
        return saleStatisticVO;
    }
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1. 接受pageNum=1 pageSize=10
        int pageNum = 1;
        int pageSize = 10;
        //2. 将pageNum pageSize === skip 和limit
        int skip = (pageNum-1)*pageSize;
        int limit = pageSize;
        //3. 调用productDAO查询数据 list
        ProductDAO productDAO = new ProductDAO();
        List<Product> list = productDAO.findPage(skip, limit);
        //获得products数据总条数
        long total = productDAO.getCount();
        //4. 将total list pageNum pageSize 封装--->PageBean 传给List.jsp
        PageBean<Product> page = new PageBean<>();
        page.setPageNum(pageNum);
        page.setPageSize(pageSize);
        page.setList(list);
        page.setTotal(total);
        page.setPageTotal(total,pageSize);
        req.setAttribute("page",page);
        SaleStatisticVO saleStatistic = getSaleStatistic();
        req.setAttribute("saleNum",saleStatistic.getSaleNum());
        req.setAttribute("visitNum",saleStatistic.getVisitNum());
        //5. 跳转
        req.getRequestDispatcher("/product/List.jsp").forward(req,resp);
    }
}
